using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using SlimDX.XInput;
using SlimDX;

namespace Barrage.Elements.Cameras
{
    /// <summary>
    /// A simple first person camera that can be moved with the gamepad or keyboard/mouse
    /// </summary>
    public class BasicFirstPersonCamera:FirstPersonCamera
    {
        int playerIndex = (int)UserIndex.Any;

        /// <summary>
        /// Inits the camera that will be moved by the specified player
        /// </summary>
        /// <param name="playerIndex">Player's Index that will be able to move this camera</param>
        public BasicFirstPersonCamera(int playerIndex)
            : base()
        {
            this.playerIndex = playerIndex;
        }

        /// <summary>
        /// Inits the camera that will be moved by anyone
        /// </summary>
        public BasicFirstPersonCamera()
            : base()
        {
        }

        /// <summary>
        /// \internal Updates camera position and orientation
        /// </summary>
        protected override void DoUpdate()
        {
            heading += Mouse.Instance.Shift.X * 0.002f;
            pitch += Mouse.Instance.Shift.Y * 0.002f;

            if (playerIndex != (int)UserIndex.Any)
            {
                Barrage.Global.Gamepad gamepad = Barrage.Global.Gamepad.Instance((UserIndex)playerIndex);
                Vector2 rStickPos = gamepad.StickPosition(Stick.RightStick);
                Vector2 lStickPos = gamepad.StickPosition(Stick.LeftStick);
                heading += rStickPos.X * 0.02f;
                pitch -= rStickPos.Y * 0.02f;
            
                position += ViewDir * Timer.DefaultTimer.LastInterval * lStickPos.Y / 25.0f;
                position -= LeftDir * Timer.DefaultTimer.LastInterval * lStickPos.X / 25.0f;

            }
            
            float speed = 1;
            if (Keyboard.Instance.KeyState(SlimDX.DirectInput.Key.LeftShift)) speed *= 50;

            if (Keyboard.Instance.KeyState(SlimDX.DirectInput.Key.W))
                position += ViewDir * Timer.DefaultTimer.LastInterval * speed / 500.0f;
            if (Keyboard.Instance.KeyState(SlimDX.DirectInput.Key.S))
                position -= ViewDir * Timer.DefaultTimer.LastInterval * speed / 500.0f;

            if (Keyboard.Instance.KeyState(SlimDX.DirectInput.Key.A))
                position += LeftDir * Timer.DefaultTimer.LastInterval * speed / 500.0f;
            if (Keyboard.Instance.KeyState(SlimDX.DirectInput.Key.D))
                position -= LeftDir * Timer.DefaultTimer.LastInterval * speed / 500.0f;

            base.DoUpdate();
        }
    }
}
